package com.chixing.ih.module.system.service.Impl;

import com.chixing.ih.json.StateValue;
import com.chixing.ih.module.system.service.ISystemService;
import com.chixing.ih.util.EmptyUtil;
import com.chixing.ih.util.MailUtil;
import com.chixing.ih.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 刘岩
 * @version 1.0
 * @Desc Practice makes perfect
 * @date 2020/9/22 11:13
 */
@Service
@Transactional
public class SystemServiceImpl implements ISystemService {
    @Autowired
    private RedisUtil redisUtil;

    @Value("${spring.redis.key.prefix.authCode}")
    private String REDIS_KEY_PREFIX_AUTH_CODE;

    @Autowired
    MailUtil mailUtil;

    public static final String REGISTER_TEMPLATE = "SMS_202345085";
    //对输入的验证码进行校验
    @Override
    public int verifyAuthCode(String authCode, String telephone) {
        boolean expire = redisUtil.isExpire(REDIS_KEY_PREFIX_AUTH_CODE + telephone);
        System.out.println(REDIS_KEY_PREFIX_AUTH_CODE + telephone);
        String realAuthCode = (String) redisUtil.get(REDIS_KEY_PREFIX_AUTH_CODE + telephone);
        if (EmptyUtil.isEmpty(authCode) || !authCode.equals(realAuthCode)) {
            if (expire) {
                //过期
                return StateValue.STATE_VALUE_ONE;
            }
            //验证码错误
            return StateValue.STATE_VALUE_TWO;

        } else {
            //验证码正确
            return StateValue.STATE_VALUE_THREE;
        }
    }


}
